package right_content_config

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"gitee.com/huanminabc/go-utils/db"
	"go-CodeToolUi/core"
	"go-CodeToolUi/utils"
)

type MysqlConfig struct {
	root     fyne.CanvasObject
	host     *widget.Entry
	port     *widget.Entry
	user     *widget.Entry
	password *widget.Entry
}

func (a *MysqlConfig) LadeContent() {
	a.root = container.New(layout.NewVBoxLayout(),
		utils.RowEmpty(20),
		utils.Line(),
		utils.RowEmpty(20),
		a.row1(),
		utils.RowEmpty(20),
		a.row2(),
		utils.RowEmpty(20),
		a.row3(),
		utils.RowEmpty(20),
		a.row4(),
		utils.RowEmpty(20),
		utils.Line(),
		utils.RowEmpty(20),
		a.row5(),
		utils.RowEmpty(30),
	)
}

func (a *MysqlConfig) GetContent() fyne.CanvasObject {
	return a.root
}

func (a *MysqlConfig) row1() fyne.CanvasObject {
	label1 := widget.NewLabel("主机:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入主机地址"
	input.Text = core.ConfigApp.DbConfig.MysqlConfig.Host
	a.host = input
	labelPadding := utils.PaddingLeftOrRight(label1, 0, 5)
	f := container.New(layout.NewFormLayout(), labelPadding, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}

func (a *MysqlConfig) row2() fyne.CanvasObject {
	label1 := widget.NewLabel("端口:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入端口"
	input.Text = core.ConfigApp.DbConfig.MysqlConfig.Port
	a.port = input
	labelPadding := utils.PaddingLeftOrRight(label1, 0, 5)
	f := container.New(layout.NewFormLayout(), labelPadding, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}

func (a *MysqlConfig) row3() fyne.CanvasObject {
	label1 := widget.NewLabel("用户名:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入用户名"
	input.Text = core.ConfigApp.DbConfig.MysqlConfig.User
	a.user = input
	f := container.New(layout.NewFormLayout(), label1, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}

func (a *MysqlConfig) row4() fyne.CanvasObject {
	label1 := widget.NewLabel("密码:")
	input := widget.NewPasswordEntry()
	input.PlaceHolder = "请输入密码"
	input.Text = core.ConfigApp.DbConfig.MysqlConfig.Password
	a.password = input
	labelPadding := utils.PaddingLeftOrRight(label1, 0, 5)
	f := container.New(layout.NewFormLayout(), labelPadding, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}

func (a *MysqlConfig) row5() fyne.CanvasObject {

	button1 := widget.NewButton("测试", func() {
		connectLogo := db.ConnectLogo{
			Type:   db.MYSQL,
			User:   a.user.Text,
			Passwd: a.password.Text,
			Host:   a.host.Text,
			Port:   a.port.Text,
		}
		getDb := db.GetDb(connectLogo)
		if getDb == nil {
			utils.ErrorDialog("连接失败", "数据库连接失败", core.ConfigWindow)
		} else {
			defer getDb.Close()
			utils.InfoDialog("连接成功", "数据库连接成功", core.ConfigWindow)
		}

	})

	button2 := widget.NewButton("保存", func() {
		core.ConfigApp.DbConfig.MysqlConfig = core.MysqlConfigEntity{
			Host:     a.host.Text,
			Port:     a.port.Text,
			User:     a.user.Text,
			Password: a.password.Text,
		}
		core.SaveConfig() //保存配置
		utils.InfoDialog("保存成功", "配置保存成功", core.ConfigWindow)
	})
	f := container.New(layout.NewFormLayout(), button1, button2)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}
